package org.apache.myfaces.custom.ysf.checkbox;

import java.io.IOException;
import java.util.Iterator;
import java.util.Set;

import javax.faces.component.UIComponent;
import javax.faces.component.UISelectItems;
import javax.faces.component.UISelectMany;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.model.SelectItem;

import org.apache.myfaces.custom.checkbox.HtmlCheckbox;
import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;

public class HtmlCheckboxRenderer extends org.apache.myfaces.renderkit.html.ext.HtmlCheckboxRenderer {

	protected void renderSingleCheckbox(FacesContext facesContext, HtmlCheckbox checkbox) throws IOException
	  {
	    String forAttr = checkbox.getFor();
	    if (forAttr == null)
	    {
	      throw new IllegalStateException("mandatory attribute 'for'");
	    }
	    int index = checkbox.getIndex();
	    if (index < 0)
	    {
	      throw new IllegalStateException("positive index must be given");
	    }

	    UIComponent uiComponent = checkbox.findComponent(forAttr);
	    if (uiComponent == null)
	    {
	      throw new IllegalStateException("Could not find component '" + forAttr + "' (calling findComponent on component '" + checkbox.getClientId(facesContext) + "')");
	    }
	    if (!(uiComponent instanceof UISelectMany))
	    {
	      throw new IllegalStateException("UISelectMany expected");
	    }

	    UISelectMany uiSelectMany = (UISelectMany)uiComponent;
	    Converter converter = getConverter(facesContext, uiSelectMany);
	    
	    SelectItem[] selectItemList = null;
	    if ( index>0 )
	    	selectItemList = (SelectItem[])uiSelectMany.getAttributes().get("YSF_RESOLVED_LIST");
	    
	    if ( selectItemList==null ) {
	    
	    //List selectItemList = RendererUtils.getSelectItemList(uiSelectMany);
	    Iterator<?> _childs = uiSelectMany.getChildren().iterator();
	    if (_childs.hasNext()){
            UIComponent child = (UIComponent) _childs.next();
            while (!(child instanceof UISelectItems)) {
                //Try to skip it
                if (_childs.hasNext()) {
                    //Skip and do the same check
                    child = (UIComponent) _childs.next();
                } else {
                	throw new IllegalStateException("SelectItems expected.");
                }
            }
            UISelectItems _currentUISelectItems = ((UISelectItems) child);
            selectItemList = (SelectItem[])_currentUISelectItems.getValue();
            uiSelectMany.getAttributes().put("YSF_RESOLVED_LIST", selectItemList );
        }    
	    }
            if (index >= selectItemList.length)
    	    {
    	      throw new IndexOutOfBoundsException("index " + index + " >= " + selectItemList.length);
    	    }
            SelectItem selectItem = (SelectItem)selectItemList[index];
    	    Object itemValue = selectItem.getValue();
    	    String itemStrValue = getItemStringValue(facesContext, uiSelectMany, converter, itemValue);

    	    Set<?> lookupSet = RendererUtils.getSelectedValuesAsSet(facesContext, uiComponent, converter, uiSelectMany);

    	    renderCheckbox(facesContext, uiSelectMany, itemStrValue, isDisabled(facesContext, uiSelectMany), lookupSet.contains(itemStrValue), true, 0);

        
   	  }




}
